#include"stack.h"
void STInit(ST* ps)
{
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;

}
void STDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->capacity = ps->top = 0;


}
void STPush(ST* ps, STDataType x)
{
	assert(ps);
	STDataType newcapacity = 0;
	
	if (ps->capacity == ps->top)
	{
		newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* temp = (STDataType*)realloc(ps->a, sizeof(ST) * newcapacity);
		if (temp == NULL)
		{
			perror("realloc:");
			exit(-1);
		}
		ps->a = temp;
		ps->capacity = newcapacity;

	}
	//ps->a = temp;
	ps->a[ps->top] = x;
	ps->capacity = newcapacity;
	ps->top++;


}
void  STPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	--ps->top;


}
STDataType STTop(ST* ps)
{

	assert(ps);
	assert(ps->top > 0);
	return ps->a[ps->top - 1];

}

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}
bool STEmpty(ST* ps)
{
	return ps->top == 0;
}